<?php
/**
* 名称：产品模块控制器。
* 描述：产品模块相关的控制响应程式。
*/
final class Controller_Product extends Controller{
    /**
    * 描述：列表视图程式。
    * @access protected
    * @return void
    */
    protected function Listview(){
        $this->Data['category']=$this->CategoryData();
        $this->Data['product']=$this->CategoryData(TRUE);
        //~
        self::$Template->Assign('data',$this->Data);
        self::$Template->Display('product/list.html');
    }

    /**
    * 描述：详细信息程式。
    * @access protected
    * @return void
    */
    protected function Detail(){
        $id=Route::GetArguments('id');
        //~
        $this->Data['category']=$this->CategoryData(TRUE);
        $this->Data['product']=self::$Database->GetRow(
            'SELECT * FROM '.self::$Database->Table('product').' WHERE id=?',
            array($id)
        );
        //~
        $this->Data['meta']=array(
            'category_name'=>self::$Database->GetOne(
                'SELECT name FROM '.self::$Database->Table('product_category').' WHERE id=?',
                array($this->Data['product']['category'])
            )
        );
        //~
        self::$Template->Assign('data',$this->Data);
        self::$Template->Display('product/detail.html');
    }

    /**
    * 描述：辅助列表视图程式之获取所有商品数据。
    * @access protected
    * @return void
    */
    private function CategoryData($isLoadProduct=FALSE){
        $data=self::$Database->GetAll(
            'SELECT * FROM '.self::$Database->Table('product_category').' ORDER BY id ASC'
        );
        //~
        if($isLoadProduct){
            foreach($data as &$value){
                $value['product']=self::$Database->GetAll(
                    'SELECT * FROM '.self::$Database->Table('product').' WHERE category=? ORDER BY id ASC',
                    array($value['id'])
                );
            }
        }
        return $data;
    }
}